package com.example.myfirstapplication

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.example.myfirstapplication.adapter.BannerAdapter
import com.example.myfirstapplication.adapter.CommentRecyclerAdapter
import com.example.myfirstapplication.data.News
import com.example.myfirstapplication.utils.BaseActivity
import com.example.myfirstapplication.utils.Tag
import com.example.myfirstapplication.utils.myToast
import com.njp.library.controller.AutoPlayController
import com.njp.library.controller.AutoPlayMode
import com.njp.library.indicator.DrawableIndicator
import kotlinx.android.synthetic.main.activity_recyclerview_layout.*
import kotlinx.android.synthetic.main.newslistitem_layout.view.*

class RecyclerViewActivity : BaseActivity() {
    private var newsList: ArrayList<News>? = ArrayList()
    private var bannerData: ArrayList<Map<String, String>>? = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview_layout)
        recycler_topBar.setLeftBtnText("<返回")
        recycler_topBar.setLeftBtnOnClickCallback {
            val intentObj = Intent(this, MainActivity::class.java)
            startActivity(intentObj)
            finish()
        }
        recycler_topBar.hideLeftBar()
        recycler_topBar.setTitle("RecyclerView使用")
        initNews()
        //recyclerView.layoutManager = LinearLayoutManager(this)//类似于listView
        //recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)//类似于listView
        /*        val sglm = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)//类似于瀑布流
        sglm.reverseLayout = true
        recyclerView.layoutManager = sglm*/
        //recyclerView.layoutManager = GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, true)//类似宫格
        //recyclerView.adapter = NewsRecyclerListAdapter(this, newsList!!)
        recyclerView.adapter =
            CommentRecyclerAdapter.Builder()
                .setData(newsList!!).setLayout(R.layout.newslistitem_layout)
                .bindView { holder, innerData ->
                    val data = innerData as News
                    holder.itemView.news_Title.text = data.title
                    Glide.with(this).load(data.thumb).into(holder.itemView.news_Thumb)
                    holder.itemView.news_Title.setOnClickListener {
                        Log.i(Tag, "点击了${data}")
                        myToast(
                            this,
                            "你点击的是${data}"
                        )
                    }
                }.create()
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true)

        val bannerAdapter = BannerAdapter(this, bannerData!!)
        banner.adapter = bannerAdapter
        AutoPlayController().enableAutoPlay(true).setAutoPlayMode(AutoPlayMode.VISIBLE)
            .setInterval(5000).setLifecycleOwner(this).setViewPager2(banner)
        val indicator = findViewById<DrawableIndicator>(R.id.indicator)
        indicator.setupWithViewPager2(banner)
    }

    private fun initNews() {
        for (i in 1..30) {
            val news = News(
                "$i",
                "第${i}条新闻",
                "http://www.jci.edu.cn/images/P420200501.jpg"
            )
            newsList?.add(news)
        }

        bannerData?.add(
            mapOf(
                "url" to "https://th.wallhaven.cc/lg/vg/vg3wm5.jpg",
                "title" to "title1"
            )
        )
        bannerData?.add(
            mapOf(
                "url" to "https://th.wallhaven.cc/lg/5w/5w6j85.jpg",
                "title" to "title2"
            )
        )
        bannerData?.add(
            mapOf(
                "url" to "https://th.wallhaven.cc/lg/5w/5w6319.jpg",
                "title" to "title3"
            )
        )
        bannerData?.add(
            mapOf(
                "url" to "https://th.wallhaven.cc/lg/ym/ymwggg.jpg",
                "title" to "title4"
            )
        )
    }
}